var defaultDate = document.getElementById('dateIn');
defaultDate.valueAsDate = new Date();
var $t = $('#taskIn');
var $d = $('#dateIn');
$(document).ready(function (){
            $.ajax({
                url:'/task/show-task/',
                dataType:'JSON',
                type:'POST',
                success:function (resp){
                    // 写入div文件中
                    for(var i=0;i<resp.data.length;i++){
                        var task = $("<div class='task' style='float: left'></div>");
                        task.text(resp.data[i].body);
                        task.attr({'id':resp.data[i].id})
                        var endDate = new Date(resp.data[i].deadline);
                        var nowDate = new Date();
                        var gap = new Date(endDate - nowDate).getDay();
                        if(gap < 3 && !Boolean(resp.data[i].status)){
                            task.css('background','rgba(255,144,122,0.5)');
                        }
                        var date = $('<div style="font-weight: bold;font-size: medium;float:left;padding-right: 5px"></div>');
                        date.text(resp.data[i].deadline);
                        var del = $("<i class='fas fa-trash-alt'></i>").click(function() {
                            $.ajax({
                                url: '/task/del-task/',
                                type: 'POST',
                                data: {"t_id": $(this).parent().attr('id')},
                                success: function (resp) {
                                    location.reload();
                                },
                            });
                            var p = $(this).parent();
                            p.fadeOut(function () {
                                p.remove();
                            });
                        })
                        var check = $("<i class='fas fa-check'></i>").click(function(){
                            $.ajax({
                              url:'/task/task-check/',
                              data:{"t_id":$(this).parent().attr('id')},
                              type:'POST',
                              success:function (){
                                 location.reload();
                              },
                            });
                            var p = $(this).parent();
                            p.css('background','');
                            p.fadeOut(function(){
                              $(".comp").append(p);
                              p.fadeIn();
                            });
                            $(this).remove();
                      })
                          if(!Boolean(resp.data[i].status) && endDate>nowDate){
                              task.append(date,del,check);
                              $(".notcomp").append(task);

                          }else{
                              task.append(del,date);
                              $(".comp").append(task);
                          }
                          $t.val("");}}})})
$t.on('keyup',function (e){
    if(e.keyCode === 13 && $t.val() !== ""){
        $.ajax({
            url:'/task/add-task/',
            type:'POST',
            data:{'body':$t.val(),'ddl':$d.val()},
            success:function (){
                location.reload();}})}})
